package fenyu.personblob.web.service;

import fenyu.personblob.bean.databean.User;
import fenyu.personblob.bean.databean.user.AdminUser;
import fenyu.personblob.bean.strategy.Strategy;
import fenyu.personblob.error.NotFoundError;
import fenyu.personblob.single.SinglePathDoor;
import fenyu.personblob.web.model.ApiReponse;
import fenyu.personblob.web.model.LoginData;
import fenyu.personblob.web.model.UserShow;
import fenyu.personblob.web.util.TokenGetUserStrategy;
import fenyu.personblob.web.util.TokenMakerStrategy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * create at 2018/12/22 10:33
 *
 * @author 纷羽 <gzj0078@gmail.com>
 */
@Service
public class UserService {

    private Strategy tokenMakerStrategy = new TokenMakerStrategy();

    private Strategy tokenGetUserStrategy = new TokenGetUserStrategy();

    /**
     * 注册用户
     *
     * @param username 用户名
     * @param password 密码
     * @param nickname 昵称
     * @return 带有用户详细信息的user
     */
    public ApiReponse<UserShow> register(String username, String password, String nickname) {
        if (SinglePathDoor.getSingleSave().getSaveDoor().findByName(username) != null) {

            return new ApiReponse<>(5001, "用户已被注册");
        }
        User user = (User) SinglePathDoor.getSingleFuns().getAutoFactory().createUser("custom");
        user.setUsername(username);
        user.setPassword(password);
        user.setNickname(nickname);
        user = SinglePathDoor.getSingleSave().getSaveDoor().getUserSavePath().save(user);
        return new ApiReponse<>(new UserShow(user));
    }

    /**
     * 登陆认证
     *
     * @param username 用户名
     * @param password 密码
     * @return 带有token的返回体
     */
    public ApiReponse<LoginData> login(String username, String password) {
        User user = SinglePathDoor.getSingleSave().getSaveDoor().findByName(username);
        if (user == null || !user.getPassword().equals(password))
            return new ApiReponse<LoginData>(503, "wrong username or password");
        LoginData data = new LoginData(user, (String) tokenMakerStrategy.todo(user));
        return new ApiReponse<LoginData>(data);
    }

    public ApiReponse<UserShow> trytoken(String token) {
        User user = (User) tokenGetUserStrategy.todo(token);
        if (user == null) {
            return new ApiReponse<>(503, "no login");
        }
        return new ApiReponse<>(new UserShow(user));
    }

    public ApiReponse<List<UserShow>> getuserlist(String token) {
        User user = (User) tokenGetUserStrategy.todo(token);
        if (user == null) {
            SinglePathDoor.getSingleSave().getSaveDoor().createlog(0, "成功拦截一条获取用列表的非法请求");
            return new ApiReponse<>(503, "no power");
        }
        List<User> users = SinglePathDoor.getSingleSave().getSaveDoor().getUserSavePath().getAll();
        List<UserShow> userShowList = new ArrayList<>();
        for (User u : users) {
            userShowList.add(new UserShow(u));
        }
        return new ApiReponse<>(userShowList);
    }

    public ApiReponse changePower(String token, String type, Integer aim) {
        User user = (User) tokenGetUserStrategy.todo(token);
        if (!(user instanceof AdminUser)) {
            return new ApiReponse<>(503, "no power");
        }
        try {
            ((AdminUser) user).changeUserPower(aim, type);
            return new ApiReponse<>("ok");
        } catch (NotFoundError e) {
            return new ApiReponse<>(401, "没有目标");
        }
    }

    public ApiReponse printLog(String token) {
        User user = (User) tokenGetUserStrategy.todo(token);
        if (!(user instanceof AdminUser)) {
            return new ApiReponse<>(503, "no power");
        } else {
            ((AdminUser) user).logCommand();
            return new ApiReponse<>("ok");
        }
    }
}
